package me.zhengjie.server.service.impl;

import me.zhengjie.server.domain.ServerInfo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.server.repository.ServerInfoRepository;
import me.zhengjie.server.service.ServerInfoService;
import me.zhengjie.server.service.dto.ServerInfoDTO;
import me.zhengjie.server.service.dto.ServerInfoQueryCriteria;
import me.zhengjie.server.service.mapper.ServerInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author yhg
* @date 2019-11-25
*/
@Service
@CacheConfig(cacheNames = "serverInfo")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ServerInfoServiceImpl implements ServerInfoService {

    private final ServerInfoRepository serverInfoRepository;

    private final ServerInfoMapper serverInfoMapper;

    public ServerInfoServiceImpl(ServerInfoRepository serverInfoRepository, ServerInfoMapper serverInfoMapper) {
        this.serverInfoRepository = serverInfoRepository;
        this.serverInfoMapper = serverInfoMapper;
    }

    @Override
    @Cacheable
    public Map<String,Object> queryAll(ServerInfoQueryCriteria criteria, Pageable pageable){
        Page<ServerInfo> page = serverInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(serverInfoMapper::toDto));
    }

    @Override
    @Cacheable
    public List<ServerInfoDTO> queryAll(ServerInfoQueryCriteria criteria){
        return serverInfoMapper.toDto(serverInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Cacheable(key = "#p0")
    public ServerInfoDTO findById(Long id) {
        ServerInfo serverInfo = serverInfoRepository.findById(id).orElseGet(ServerInfo::new);
        ValidationUtil.isNull(serverInfo.getId(),"ServerInfo","id",id);
        return serverInfoMapper.toDto(serverInfo);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ServerInfoDTO create(ServerInfo resources) {
        return serverInfoMapper.toDto(serverInfoRepository.save(resources));
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ServerInfo resources) {
        ServerInfo serverInfo = serverInfoRepository.findById(resources.getId()).orElseGet(ServerInfo::new);
        ValidationUtil.isNull( serverInfo.getId(),"ServerInfo","id",resources.getId());
        serverInfo.copy(resources);
        serverInfoRepository.save(serverInfo);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        serverInfoRepository.deleteById(id);
    }


    @Override
    public void download(List<ServerInfoDTO> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ServerInfoDTO serverInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("服务名称", serverInfo.getName());
            map.put("内网ip", serverInfo.getIip());
            map.put("外网ip", serverInfo.getEip());
            map.put(" port",  serverInfo.getPort());
            map.put(" remark",  serverInfo.getRemark());
            map.put("服务商：如阿里", serverInfo.getBrank());
            map.put(" type",  serverInfo.getType());
            map.put(" createTime",  serverInfo.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}